home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13000 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: rain.fr!world-net!usenet
  2. From: Frederic LACHASSE <lachass@worldnet.fr>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to simulate EOF from the keyboard in Visual C++ 4.0, CTRL+Z doesn't work
  5. Date: Fri, 22 Mar 1996 23:13:01 +0000
  6. Organization: World-Net information exchange, Internet provider.
  7. Message-ID: <VA.00000071.00e2e9a9@fred>
  8. References: <314CB927.22A3@cco.caltech.edu>
  9. Reply-To: lachass@worldnet.fr
  10. NNTP-Posting-Host: client56.sct.fr
  11. X-Newsreader: Virtual Access by Ashmount Research Ltd, http://www.ashmount.com
  12.  
  13. In article <314CB927.22A3@cco.caltech.edu>, Xuhua Li 
  14. <xuhua@cco.caltech.edu> wrote:
  15. > The title says everything. When I try to simulate the EOF from the 
  16. > keyboard in Visual C++ with CTRL+Z, it doesn't work. CTRL+Z just kill 
  17. > the running program. I am running windows 95.
  18. > Please try the following simple program from <<C++ Primer Plus>>:
  19. > Program #1
  20. > // textin3.cpp -- reading chars to end of file
  21. > #include <iostream.h>
  22. > int main(void)
  23. > {
  24. >  char ch;
  25. >  int count = 0;
  26. >  while (cin.get(ch))      // cin.get(ch) is 0 on EOF
  27. >  {
  28. >   cout << ch;
  29. >   count++;
  30. >  }
  31. >  cout << count << " characters read\n";
  32. >  return 0;
  33. > }
  34. [second example snip]
  35.  
  36. In fact, CTRL+Z have the expected result, the problem is that cout is 
  37. buffered and Visual C++ (like most DOS C++ compilers) don't flush 
  38. buffered iostreams, so your last message and the last entered line has 
  39. few chances to be actually written to the screen.
  40.  
  41. A buffered output stream is flushed when: the buffer is full, when an 
  42. input is requested from a tied input stream (like cin and cout) or when 
  43. it is explicitly flushed using:
  44.  
  45.  cout.flush();
  46.  
  47. or:
  48.  
  49.  cout << flush;
  50.  
  51. Very useful: the endl manipulator that send a new line to the output 
  52. stream then flushes it.
  53.  
  54. So, generally we do:
  55.  
  56.  cout << count << " characters read" << endl;
  57.  
  58.  Frederic LACHASSE (ECP 86)
  59.  CompuServe: 100530,2005
  60.  Internet: lachass@worldnet.fr
  61.  
  62.